from utility import *



def calculate_volumn(utilization_vector):
    result = 1
    for u in utilization_vector:
        result = result / (1 - u)
    return result



def find_potential_server(servers, task):
    status = False
    result = None
    for s in servers:
        if status == False:
            if s.check_resource_limit(task):
                status = True
                result = s
                lowerest_volumn = calculate_volumn(result.utilization_vector())
        else:
            if  lowerest_volumn > calculate_volumn(s.utilization_vector()) \
                and s.check_resource_limit(task):
                result = s
    return result



def max_dvol(tasks):
    servers = []
    for t in tasks:
        potential_server = find_potential_server(servers, t)
        if potential_server == None:
            new_server = create_sample_server()
            new_server.add_task(t)
            servers.append(new_server)
        else:
            potential_server.add_task(t)
    return servers